package com.sharp.authorization.annotation.auth;

import com.sharp.authorization.bo.account.AccountInfo;
import com.sharp.authorization.bo.role.RoleInfo;
import com.sharp.authorization.constants.err.AccountErrCode;
import com.sharp.authorization.constants.param.AccountParam;
import com.sharp.authorization.holder.AccountContextHolder;
import com.sharp.framework.service.cache.FrameCacheService;
import com.sharp.kernel.utils.StringUtil;
import com.sharp.kernel.validate.Validator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Title: AdminAuthRole
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2022/6/15 13:22
 */
@Slf4j
@Component
public class AdminAuthRole extends LoginAuthRole {
    @Resource
    private FrameCacheService frameCacheService;

    @Override
    public void validate(HttpServletRequest request) {
        super.validate(request);
        String adminRoleId = frameCacheService.getParamValueAvail(AccountParam.ACCOUNT_ADMIN_ROLE_ID);
        AccountInfo accountInfo = AccountContextHolder.get();
        boolean isAdmin = false;
        for (RoleInfo roleInfo : accountInfo.getUserInfo().getRoleInfoMap().values()) {
            if (StringUtil.equals(roleInfo.getId(), adminRoleId)) {
                isAdmin = true;
                break;
            }
        }
        Validator.assertTrue(isAdmin, AccountErrCode.AC_IS_NOT_ADMIN);
    }
}
